package com.ruoyi.customer.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.customer.mapper.CustomerInfoMapper;
import com.ruoyi.customer.domain.CustomerInfo;
import com.ruoyi.customer.service.ICustomerInfoService;

/**
 * 客户信息Service业务层处理
 *
 * @author aef
 * @date 2022-11-27
 */
@Service
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerInfoService {

    /**
     * 查询客户信息列表
     *
     * @param customerInfo 客户信息
     * @return 客户信息
     */
    @Override
    public List<CustomerInfo> selectCustomerInfoList(CustomerInfo customerInfo) {
        return getBaseMapper().selectCustomerInfoList(customerInfo);
    }

    @Override
    public boolean saveOrUpdate(CustomerInfo customerInfo){
        List<String> customerNames = getBaseMapper().selectAllCustomerName();
        if (customerNames.contains(customerInfo.getCustomerName())){
            throw new ServiceException("该企业信息已存在");
        }
        if (customerInfo.getRegCapital().longValue() < 0){
            throw new ServiceException("金额不可以为负数");
        }
        if (customerInfo.getId() == null){
            String username = SecurityUtils.getUsername();
            customerInfo.setInputUser(username);
            customerInfo.setInputTime(new Date());
            return super.save(customerInfo);
        }else {
            return super.updateById(customerInfo);
        }
    }

}
